
//填充历史趋势图
var chart1 = null
var _date_date_ = 'sec'
// chart 1
var options = {
	series: [{
		name:'host',
		data:[]
	},{
		name:"avg",
		data:[]
	}],
	chart: {
		foreColor: '#9ba7b2',
		type: 'line',
		height: 540,
		animations: {
			enabled: false
		},
		toolbar: {
			show: true
		},
		zoom: {
			enabled: true
		},
		dropShadow: {
			enabled: false,
			top: 3,
			left: 14,
			blur: 4,
			opacity: 0.10,
		}
	},
	legend: {
		position: 'top',
		horizontalAlign: 'left',
		offsetX: -25
	},
	dataLabels: {
		enabled: false
	},
	stroke: {
		show: true,
		width: 2.3,
		//: 'smooth',
	},
	tooltip: {
		theme: 'dark',
		y: {
			formatter: function (val) {
				return val + '毫秒'
			}
		},
		x: {
			formatter: function (val) {
				return val + map[_date_date_]
			}
		}
	},
	yaxis: {
		max: 100,
		min: 0,
		labels: {
			formatter: function (value) {
				return Math.round(value) + "毫秒";
			}
		},
	},
	xaxis: {
		categories: [1,2,3,4,5,6,7,8,9,10],
		labels: {
			formatter: function (value) {
				 return value;
			}
		},
	}
};
var datas=[]
var avgs=[]
const render_char_1 = function () {
	chart1 = new ApexCharts(document.querySelector("#chart1"), options);
	chart1.render();
}
const update_char_1 = function () {
	var server = $('#serverip').val()
	$.ajax({
		type: 'GET',
		url: '/pingsvr',
		data: {
			server: server.trim()
		},
		success: function (data) {
			datas.push(data)
			if(datas.length>10)
				datas=datas.slice(datas.length-10)

			var avg=0
			for(var c of datas) avg+=c
			avg=avg/datas.length
			avgs.push(avg)
			if(avgs.length>10)
				avgs=avgs.slice(avgs.length-10)

			chart1.updateSeries([{
				data: datas
			},{data:avgs}])
		},
		dataType: 'json',
		error: function (xthr, err, msg) {
			alert(msg)
		}
	});

}
var st = null;
$(document).ready(function () {
	render_char_1()
	$('#refresh_button').click(function () {
		var server = $('#serverip').val()
		if (server.trim() == '') {
			alert('请输入服务器IP！')
			$('#serverip').focus()
			return
		}
		if ($(this).text() == "开始监测") {
			$(this).text('停止监测')
			st = setInterval("update_char_1()", 1000)
		} else {
			$(this).text('开始监测')
			clearInterval(st)
			st = null
		}
	})
});

